package com.distributor;

import java.text.DecimalFormat;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityMyAcc extends Activity 
{
	TextView tv_acc_name,tv_IdProof,tv_IDNumber,tv_billingTemp,tv_Name,
			 tv_phone1,tv_Phone2,tv_altPhone,tv_email,tv_addr1,tv_addr2,tv_areaName,
			 tvZipCode,tvState,tvStateName,tvImei,tveaderText;
	Button btnBack,btnLogOut;
	ProgressDialog pd=null;
	Handler handler=new Handler();
	MD5Generator md5Generator=new MD5Generator();
	HttpConn httpConn=new HttpConn();
	MyAccountInfo myAccountInfo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		 setContentView(R.layout.activity_my_acc);
		 tveaderText=(TextView)findViewById(R.id.tv_headerText);
		 tveaderText.setText("My Account");
		 tv_acc_name=(TextView)findViewById(R.id.tv_acc_name_agent);
	       // tv_balance=(TextView)findViewById(R.id.tv_acc_balance);
        tv_IdProof=(TextView)findViewById(R.id.tv_idProof_agent);
        tv_IDNumber=(TextView)findViewById(R.id.tv_acc_idNumber_agent);
        tv_billingTemp=(TextView)findViewById(R.id.tv_acc_billing_temp_agent);
        tv_Name=(TextView)findViewById(R.id.tv_name_agent);
        tv_phone1=(TextView)findViewById(R.id.tv_acc_phone1_agent);
        tv_Phone2=(TextView)findViewById(R.id.tv_acc_phone2_agent);
        tv_altPhone=(TextView)findViewById(R.id.tv_acc_altPhone_agent);
        tv_email=(TextView)findViewById(R.id.tv_email_agent);
        tv_addr1=(TextView)findViewById(R.id.tv_addr1_agent);
        tv_addr2=(TextView)findViewById(R.id.tv_addr2_agent);
        tv_areaName=(TextView)findViewById(R.id.tv_areaName_agent);
        tvZipCode=(TextView)findViewById(R.id.tv_zipCode_agent);
        tvState=(TextView)findViewById(R.id.tv_stateName_agent);
        tvStateName=(TextView)findViewById(R.id.tv_stateName_agent);
        tvImei=(TextView)findViewById(R.id.tv_imei_agent);
        btnLogOut=(Button)findViewById(R.id.btnLogout); 
	    btnBack=(Button)findViewById(R.id.btnPrevious);
	    
		    btnLogOut.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{
					  new AlertDialog.Builder(ActivityMyAcc.this)
					  .setTitle( "Exit Application" )
				      .setMessage( "Are you sure you want to Logout?" )
				      .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
				       {
				           public void onClick(DialogInterface arg0, int arg1) 
				           {
				        	   setResult(1);
				               finish();
				           }

						   public void onClick(View v) 
						   {
							 // TODO Auto-generated method stub
						   }
						   
				        })
				        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() 
				        {
				            public void onClick(DialogInterface arg0, int arg1)
				            {
				                //do stuff onclick of CANCEL
				            	arg0.dismiss();
				            }

							public void onClick(View v) 
							{
								// TODO Auto-generated method stub
							}
				        }).show();
			  	}
				
			 });  

		    btnBack.setOnClickListener(new OnClickListener() 
		    {
				
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
				finish();	
				}
			});
	        findViewById(R.id.layout_salesEx).setVisibility(LinearLayout.GONE);
	          
	        pd=ProgressDialog.show(ActivityMyAcc.this,"please wait.",null);
	        pd.setContentView(R.layout.progress);
	        
	        Thread thread=new Thread(getPersonalInfo);
	        thread.start();
	        
	    }
		double roundTwoDecimals(double d) 
		{
	        DecimalFormat twoDForm = new DecimalFormat("#.##");
	        return Double.valueOf(twoDForm.format(d));
		}
		Runnable getPersonalInfo=new Runnable()
		{
			
			public void run() 
			{
				try
				{
					String checksum=md5Generator.MD5(md5Generator.SHA1(AppData.getUserInfo().getLoginID()+"|"+
								 	AppData.getUserInfo().getpublickey() ));
					 String URL=AppData.hostURL+"/WebService/iTopUp/distributorapp.php/GetPersonalInfo/LoginId/"+AppData.getUserInfo().getLoginID()
	                         +"/Checksum/"+checksum;
	              System.out.println("URL===>\n"+URL);
	              String response=httpConn.createHttpConn(URL);
	              System.out.println("Response:"+response);
	              myAccountInfo=new MyAccountInfo();
	              final JSONObject jsonObject=new JSONObject(response);

	              if(jsonObject.getString("ResponseCode").equals("000"))
	              {
	                   if(jsonObject.has("AccountName"))
	                      myAccountInfo.setAccountName(jsonObject.getString("AccountName"));
	                   else
	                      myAccountInfo.setAccountName("Not Avilable");
	                   
	                   if(jsonObject.has("Balance"))
	                        myAccountInfo.setBalance(""+Double.parseDouble(jsonObject.getString("Balance"))/100);
	                    
	                   else
	                       myAccountInfo.setBalance("Not Avilable");

	                   if(jsonObject.has("IDProofType"))
	                	   myAccountInfo.setIDProofType(jsonObject.getString("IDProofType"));
	                   else
	                	   myAccountInfo.setIDProofType("Not Avilable");
	                  
	                   if(jsonObject.has("IDNumber"))
	                	   myAccountInfo.setIDNumber(jsonObject.getString("IDNumber"));
	                   else
	                		   myAccountInfo.setIDNumber("Not Avilable");
	                   if(jsonObject.has("BillingTemplate"))
	                	   myAccountInfo.setBillingTemplate(jsonObject.getString("BillingTemplate"));
	                   else
	                	   myAccountInfo.setBillingTemplate("Not Avilable");
	                	   
	                   if(jsonObject.has("Name"))
	                	   myAccountInfo.setName(jsonObject.getString("Name"));
	                	   else
	                		   myAccountInfo.setName("Not Avilable");
	                   if(jsonObject.has("Phone1"))
	                	   myAccountInfo.setPhone1(jsonObject.getString("Phone1"));
	                	   else
	                		   myAccountInfo.setPhone1("not Avilable");
	                   if(jsonObject.has("Phone2"))
	                	   myAccountInfo.setPhone2(jsonObject.getString("Phone2"));
	                   else
	                	   myAccountInfo.setPhone2("Not Avilable");
	                   
	                   if(jsonObject.has("AltPhone"))
	                	   myAccountInfo.setAltPhone(jsonObject.getString("AltPhone"));
	                   else
	                	   myAccountInfo.setAltPhone("Not Avilable");
	                   
	                   if(jsonObject.has("Email"))
	                          myAccountInfo.setEmail(jsonObject.getString("Email"));
	                   else
	                          myAccountInfo.setEmail("Not Avilable");
	                   
	                   if(jsonObject.has("Address1"))
	                          myAccountInfo.setAddress1(jsonObject.getString("Address1"));
	                   else
	                       myAccountInfo.setAddress1("Not Avilable");
	                   if(jsonObject.has("Address2"))
	                      myAccountInfo.setAddress2(jsonObject.getString("Address2"));
	                   else
	                        myAccountInfo.setAddress2("Not Avilable");
	                   
	                   if(jsonObject.has("AreaName"))
	                	   myAccountInfo.setAreaName(jsonObject.getString("AreaName"));
	                   else
	                	   myAccountInfo.setAreaName("Not Avilable");
	                   if(jsonObject.has("AreaName"))
	                      myAccountInfo.setAreaName(jsonObject.getString("AreaName"));
	                   else
	                        myAccountInfo.setAreaName("Not Avilable");
	                   
	                   if(jsonObject.has("AreaZipCode"))
	                	   myAccountInfo.setAreaZipCode(jsonObject.getString("AreaZipCode"));
	                	   else
	                		   myAccountInfo.setAreaZipCode("Not Avilable");
	                   if(jsonObject.has("State"))
	                	   myAccountInfo.setState(jsonObject.getString("State"));
	                	   else
	                		   myAccountInfo.setState("Not Avilable");
	                   if(jsonObject.has("StateName"))
	                	   myAccountInfo.setStateName(jsonObject.getString("StateName"));
	                   else
	                	   myAccountInfo.setStateName("Not Avilable");
	                   if(jsonObject.has("IMEI"))
	                	   myAccountInfo.setIMEI(jsonObject.getString("IMEI"));
	                   else
	                	   myAccountInfo.setIMEI("Not Avilable");

	                   System.out.println("========="+
	                                      "\nName:"+myAccountInfo.getAccountName()+
	                                      "\nBalance:"+myAccountInfo.getBalance()+
	                                      "\nEmail:"+myAccountInfo.getEmail()+
	                                      "\nAddress1:"+myAccountInfo.getAddress1()+
	                                      "\nAddress2:"+myAccountInfo.getAddress2()+
	                                      "\nPhone:"+myAccountInfo.getPhone1());
	                   handler.post(new Runnable()
	                   { 
							public void run() 
							{
								// TODO Auto-generated method stub
								tv_acc_name.append(myAccountInfo.getAccountName());
								//tv_balance.append(myAccountInfo.getBalance());
								tv_IdProof.append(myAccountInfo.getIDProofType());
								tv_IDNumber.append(myAccountInfo.getIDNumber());
								tv_billingTemp.append(myAccountInfo.getBillingTemplate());
								tv_Name.append(myAccountInfo.getName());
								tv_phone1.append(myAccountInfo.getPhone1());
								tv_Phone2.append(myAccountInfo.getPhone2());
								tv_altPhone.append(myAccountInfo.getAltPhone());	
								tv_email.append(myAccountInfo.getEmail());
								tv_addr1.append(myAccountInfo.getAddress1());
								tv_addr2.append(myAccountInfo.getAddress2());
								tv_areaName.append(myAccountInfo.getAreaName());
								tvZipCode.append(myAccountInfo.getAreaZipCode());
								tvState.append(myAccountInfo.getState());
								tvStateName.append(myAccountInfo.getStateName());
								tvImei.append(myAccountInfo.getIMEI());
								pd.dismiss();
							}
	                   }); 
	              }
	              else
	              {
	            	  handler.post(new Runnable()
	            	  {
						
						public void run() 
						{
							// TODO Auto-generated method stub
							pd.dismiss();
							try 
							{
								Toast.makeText(ActivityMyAcc.this,jsonObject.getString("ResponseDescription"),Toast.LENGTH_SHORT).show();
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
	              }

				}//try
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
		};//getpersonalInfo
	}
